package vn.isolar.system.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import vn.isolar.main.SvntestActivity;
import vn.isolar.object.DetailRouting;
import vn.isolar.util.direction.Direction;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class ServiceTrackLocation extends Service {
	DetailRouting searchResults;
	URL urlLink;
	LocationListener mlocListener;
	LocationManager mlocManager;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {

		mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		mlocListener = new MyLocationListener();
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				mlocListener);

	}

	@Override
	public void onDestroy() {

	}

	@Override
	public void onStart(Intent intent, int startid) {
		Direction direc = new Direction();
		searchResults = direc.getDirectionDetail("cầu giấy", "phạm hùng");
		Log.w("ket qua",searchResults.getStartAddr()+ " "+searchResults.getEndAddr());
	}

	public float calDistance(double srcLng, double srclat, double desLng,
			double desLat) {

		int R = 6371;
		double x = (desLng - srcLng) * Math.cos((desLat + srclat) / 2);
		double y = (desLat - srclat);
		double d = Math.sqrt(x * x + y * y) * R;
		Location locationA = new Location("point A");

		locationA.setLatitude(srclat);
		locationA.setLongitude(srcLng);

		Location locationB = new Location("point B");

		locationB.setLatitude(desLat);
		locationB.setLongitude(desLng);
		Log.w("distence", locationA.distanceTo(locationB) + "");
		return locationA.distanceTo(locationB);
		
	}

	public class MyLocationListener implements LocationListener {
		Bundle b = null;

		public void onLocationChanged(Location loc) {
			Log.i("**************************************", "Location changed");
			
			if (b == null) {
				Log.w("test","b == nulll");
				b = searchResults.moveToNext();
				String text = b.getString("instructions").replaceAll("\\s+", " ");
				text = text.replaceAll("(\\?.*?)", "");
				SvntestActivity.tomClient.execute(text);
				Log.w("test",text);
			} else if (b.getBundle("end") != null) {
				Log.w("test","b.getBundle(end) == null");
				float d = calDistance(loc.getLongitude(), loc.getLatitude(), b
						.getBundle("start_location").getDouble("lng"), b
						.getBundle("start_location").getDouble("lat"));
				if (d < 200) {
					b = searchResults.moveToNext();
					SvntestActivity.tomClient.execute("Phia truoc 100 met la diem den");
				}
				if (d < 50) {
					SvntestActivity.tomClient.execute("Ban da den dich");
					mlocManager.removeUpdates(mlocListener);
				}
			} else{
				Log.w("test","con lai");
				double d = calDistance(loc.getLongitude(), loc.getLatitude(), b
						.getBundle("start_location").getDouble("lng"), b
						.getBundle("start_location").getDouble("lat"));
				Log.w("lng",b
						.getBundle("start_location").getDouble("lng")+ " "+b
						.getBundle("start_location").getDouble("lat"));
				
				if (d < 500) {
				b = searchResults.moveToNext();
				String text = b.getString("instructions").replaceAll("\\s+", " ");
				Log.w("test",text);
				SvntestActivity.tomClient.execute(text);
				}
			}
		}

		public void onProviderDisabled(String provider) {
			Toast.makeText(getApplicationContext(), "Gps Disabled",
					Toast.LENGTH_SHORT).show();
		}

		public void onProviderEnabled(String provider) {
			Toast.makeText(getApplicationContext(), "Gps Enabled",
					Toast.LENGTH_SHORT).show();
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

	}

}
